package avicit.bdp.dgs.qa.rest;

import avicit.bdp.common.dto.approval.ApprovalDTO;
import avicit.bdp.dgs.qa.dto.*;
import avicit.bdp.dgs.qa.service.RuleTemplateService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间：2023-03-15
 * @类说明：RuleTemplateRest
 * @修改记录：
 * @注意事项：
 * @主要功能：规则模板Rest
 */
@RestController
@Api(tags = "qa", description = "规则模板Rest接口")
@RequestMapping("/api/bdp/dgs/qa/ruleTemplate")
public class RuleTemplateRest {
    @Autowired
    private RuleTemplateService ruleTemplateService;

    /**
     * 新增规则模板
     *
     * @param ruleTemplateDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增规则模板")
    public ResponseMsg<Boolean> addRuleTemplate(@RequestBody RuleTemplateDTO ruleTemplateDTO) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleTemplateService.addRuleTemplate(ruleTemplateDTO));

        return responseMsg;
    }

    /**
     * 删除/批量删除规则模板，多个id使用英文逗号隔开
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除/批量删除规则模板，多个id使用英文逗号隔开")
    public ResponseMsg<Integer> deleteRuleTemplate(
            @ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleTemplateService.deleteRuleTemplate(id));

        return responseMsg;
    }

    /**
     * 修改规则模板
     *
     * @param ruleTemplateDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改规则模板")
    public ResponseMsg<Boolean> updateRuleTemplate(@RequestBody RuleTemplateDTO ruleTemplateDTO) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleTemplateService.updateRuleTemplate(ruleTemplateDTO));

        return responseMsg;
    }

    /**
     * 根据主键ID查询规则模板详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "根据主键ID查询规则模板详情")
    public ResponseMsg<RuleTemplateDTO> getRuleTemplateDetail(
            @ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<RuleTemplateDTO> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleTemplateService.getRuleTemplateDetail(id));

        return responseMsg;
    }

    /**
     * 分页查询规则模板
     *
     * @param measureType
     * @param templateType
     * @param ruleType
     * @param ruleDimension
     * @param keyWords
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/query")
    @ApiOperation(value = "按条件分页查询")
    public ResponseMsg<QueryRespBean<RuleTemplateDTO>> queryRuleTemplate(
            @ApiParam(value = "校验类型", name = "measureType") @RequestParam Integer measureType,
            @ApiParam(value = "模板类型", name = "templateType") @RequestParam(required = false) Integer templateType,
            @ApiParam(value = "规则类型", name = "ruleType") @RequestParam(required = false) Integer ruleType,
            @ApiParam(value = "维度", name = "ruleDimension") @RequestParam(required = false) Integer ruleDimension,
            @ApiParam(value = "查询关键字", name = "keyWords") @RequestParam(required = false) String keyWords,
            @ApiParam(value = "当前页", name = "pageNo") @RequestParam(defaultValue = "1", required = false) Integer pageNo,
            @ApiParam(value = "每页大小", name = "pageSize") @RequestParam(defaultValue = "10", required = false) Integer pageSize) {
        ResponseMsg<QueryRespBean<RuleTemplateDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(
                this.ruleTemplateService.queryRuleTemplate(measureType, templateType, ruleType, ruleDimension, keyWords, pageNo, pageSize));

        return responseMsg;
    }

    /**
     * 判读模板名称是否重复 注意： 1、同一分类层架下，模板名字不能重复
     *
     * @param ruleId      规则模板ID
     * @param measureType
     * @param name
     * @return
     */
    @GetMapping("/isSameName")
    @ApiOperation(value = "判读模板名称是否重复")
    public ResponseMsg<Boolean> isSameName(
            @ApiParam(value = "模板ID", name = "ruleId") @RequestParam(required = false) String ruleId,
            @ApiParam(value = "校验类型", name = "measureType") @RequestParam Integer measureType,
            @ApiParam(value = "名字", name = "name") @RequestParam String name) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleTemplateService.isSameName(ruleId, measureType, name));

        return responseMsg;
    }

    /**
     * 获取规则模板版本
     *
     * @param id
     * @return
     */
    @GetMapping("/getVersion")
    @ApiOperation(value = "获取规则模板版本")
    public ResponseMsg<String> getVersion(
            @ApiParam(value = "模板ID", name = "id") @RequestParam String id) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleTemplateService.getVersion(id));

        return responseMsg;
    }

    /**
     * 发布新版本
     *
     * @param id
     * @param version
     * @return
     */
    @PostMapping("/publish")
    @ApiOperation(value = "发布新版本")
    public ResponseMsg<Boolean> publishRuleTemplate(
            @ApiParam(value = "模板ID", name = "id") @RequestParam String id,
            @ApiParam(value = "版本", name = "version") @RequestParam String version) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleTemplateService.publishRuleTemplate(id, version));

        return responseMsg;
    }

    /**
     * 版本下线
     *
     * @param id
     * @return
     */
    @PostMapping("/offline")
    @ApiOperation(value = "版本下线")
    public ResponseMsg<Boolean> offline(
            @ApiParam(value = "模板ID", name = "id") @RequestParam String id) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleTemplateService.offline(id));

        return responseMsg;
    }

    /**
     * 删除/批量删除模板历史版本，多个id使用英文逗号隔开
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteHistory")
    @ApiOperation(value = "删除/批量删除模板历史版本，多个id使用英文逗号隔开")
    public ResponseMsg<Integer> deleteRuleTemplateHistory(
            @ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleTemplateService.deleteRuleTemplateHistory(id));

        return responseMsg;
    }

    /**
     * 获取历史版本
     *
     * @param id
     * @return
     */
    @GetMapping("/history")
    @ApiOperation(value = "获取历史版本")
    public ResponseMsg<List<RuleTemplateHistoryDTO>> getHistory(
            @ApiParam(value = "模板ID", name = "id") @RequestParam String id) {
        ResponseMsg<List<RuleTemplateHistoryDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleTemplateService.getHistory(id));

        return responseMsg;
    }

    /**
     * 根据主键ID查询历史版本详情
     *
     * @param id
     * @return
     */
    @GetMapping("/historyDetail")
    @ApiOperation(value = "根据主键ID查询历史版本详情")
    public ResponseMsg<RuleTemplateHistoryDTO> getRuleTmplHistoryDetail(
            @ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<RuleTemplateHistoryDTO> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleTemplateService.getRuleTmplHistoryDetail(id));

        return responseMsg;
    }

    /**
     * 版本对比
     *
     * @param ruleTemplateId1
     * @param ruleTemplateId2
     * @return
     */
    @GetMapping("/compare")
    @ApiOperation(value = "版本对比")
    public ResponseMsg<List<RuleTemplateHistoryDTO>> compareRuleTemplate(
            @ApiParam(value = "模板ID", name = "id") @RequestParam String ruleTemplateId1,
            @ApiParam(value = "模板ID", name = "id") @RequestParam String ruleTemplateId2) {
        ResponseMsg<List<RuleTemplateHistoryDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(
                this.ruleTemplateService.compareRuleTemplate(ruleTemplateId1, ruleTemplateId2));

        return responseMsg;
    }

    /**
     * 导入规则模板
     *
     * @param measureType
     * @param override
     * @param file
     * @return
     */
    @ApiOperation(value = "导入规则模板")
    @PostMapping("/import")
    public ResponseMsg<Boolean> importRule(@RequestParam Integer measureType, @RequestParam(required = false) Boolean override, MultipartFile file) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(
                this.ruleTemplateService.importRuleTemplate(measureType, override, file));

        return responseMsg;
    }

    /**
     * 导出规则模板
     *
     * @param fileName
     * @param id
     * @param response
     */
    @ApiOperation(value = "导出规则模板")
    @PostMapping("/export")
    public void exportRule(
            @RequestParam String fileName, @RequestParam String id, HttpServletResponse response) {
        this.ruleTemplateService.exportRuleTemplate(fileName, id, response);
    }

    /**
     * 获取规则模板分类树 注意： 1、依据“规则类型-规则维度-模板类型-模板”分类
     *
     * @param measureType
     * @param templateType
     * @param jobId
     * @return
     */
    @GetMapping("/tree")
    @ApiOperation(value = "获取规则模板分类树")
    public ResponseMsg<List<RuleTemplateDTO>> getTemplateTree(@ApiParam(value = "校验类型", name = "measureType") @RequestParam Integer measureType,
                                                              @ApiParam(value = "模板类型", name = "templateType") @RequestParam Integer templateType,
                                                              @ApiParam(value = "作业ID", name = "jobId") @RequestParam(required = false) String jobId) {
        ResponseMsg<List<RuleTemplateDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleTemplateService.getTemplateTree(measureType, templateType, jobId));

        return responseMsg;
    }

    /**
     * 接收审批中心通知
     *
     * @param approvalList
     * @return
     */
    @PostMapping("/receiveMsg")
    @ApiOperation(value = "接收审批中心通知")
    public ResponseMsg<Boolean> receiveMsgFromApprovalCenter(
            @RequestBody List<ApprovalDTO> approvalList) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(
                this.ruleTemplateService.receiveMsgFromApprovalCenter(approvalList));

        return responseMsg;
    }

    /**
     * 查询指定规则模板的引用关系
     *
     * @param id
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/relation")
    @ApiOperation(value = "查询指定规则模板的引用关系")
    public ResponseMsg<QueryRespBean<TemplateRelationDTO>> queryRuleTemplate(
            @ApiParam(value = "模板ID", name = "id") @RequestParam String id,
            @ApiParam(value = "当前页", name = "pageNo") @RequestParam(defaultValue = "1", required = false) Integer pageNo,
            @ApiParam(value = "每页大小", name = "pageSize") @RequestParam(defaultValue = "999", required = false) Integer pageSize) {
        ResponseMsg<QueryRespBean<TemplateRelationDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(
                this.ruleTemplateService.queryRuleTemplateRelationShip(id, pageNo, pageSize));

        return responseMsg;
    }

    /**
     * 查询规则模板参数
     *
     * @param templateId 规则模板ID
     * @return
     */
    @GetMapping("/arguement")
    @ApiOperation(value = "查询规则模板参数")
    public ResponseMsg<List<RuleTemplateArguementDTO>> getRuleArguementList(@ApiParam(value = "templateId", name = "templateId") @RequestParam String templateId) {
        ResponseMsg<List<RuleTemplateArguementDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleTemplateService.getRuleArguementList(templateId));

        return responseMsg;
    }

    /**
     * 正则表达式合法性校验
     *
     * @param regexp 正则表达式
     * @param value  待校验值
     * @return
     */
    @GetMapping("/regexp")
    @ApiOperation(value = "正则表达式合法性校验")
    public ResponseMsg<Boolean> checkRegexpValid(@ApiParam(value = "regexp", name = "regexp") @RequestParam String regexp,
                                                 @ApiParam(value = "value", name = "value", required = false) @RequestParam String value) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleTemplateService.checkRegexpValid(regexp, value));

        return responseMsg;
    }
}
